/*
 * Copyright (C) 2010-2015 Nektra S.A., Buenos Aires, Argentina.
 * All rights reserved. Contact: http://www.nektra.com
 *
 *
 * This file is part of Deviare
 *
 *
 * Commercial License Usage
 * ------------------------
 * Licensees holding valid commercial Deviare licenses may use this file
 * in accordance with the commercial license agreement provided with the
 * Software or, alternatively, in accordance with the terms contained in
 * a written agreement between you and Nektra.  For licensing terms and
 * conditions see http://www.nektra.com/licensing/. Use the contact form
 * at http://www.nektra.com/contact/ for further information.
 *
 *
 * GNU General Public License Usage
 * --------------------------------
 * Alternatively, this file may be used under the terms of the GNU General
 * Public License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of this
 * file.  Please visit http://www.gnu.org/copyleft/gpl.html and review the
 * information to ensure the GNU General Public License version 3.0
 * requirements will be met.
 *
 **/

#include "misc.h"
#include <stdio.h>

//-----------------------------------------------------------

LPWSTR miscGetScodeString(__in SCODE sc, __out_z WCHAR szTempBufW[32])
{
  #define MAKE_SCODE_ENTRY(sc)    { sc, L#sc }
  static const struct {
    SCODE sc;
    LPWSTR szNameW;
  } scodeTable[] = {
    MAKE_SCODE_ENTRY(S_OK),
    MAKE_SCODE_ENTRY(S_FALSE),

    MAKE_SCODE_ENTRY(CACHE_S_FORMATETC_NOTSUPPORTED),
    MAKE_SCODE_ENTRY(CACHE_S_SAMECACHE),
    MAKE_SCODE_ENTRY(CACHE_S_SOMECACHES_NOTUPDATED),
    MAKE_SCODE_ENTRY(CONVERT10_S_NO_PRESENTATION),
    MAKE_SCODE_ENTRY(DATA_S_SAMEFORMATETC),
    MAKE_SCODE_ENTRY(DRAGDROP_S_CANCEL),
    MAKE_SCODE_ENTRY(DRAGDROP_S_DROP),
    MAKE_SCODE_ENTRY(DRAGDROP_S_USEDEFAULTCURSORS),
    MAKE_SCODE_ENTRY(INPLACE_S_TRUNCATED),
    MAKE_SCODE_ENTRY(MK_S_HIM),
    MAKE_SCODE_ENTRY(MK_S_ME),
    MAKE_SCODE_ENTRY(MK_S_MONIKERALREADYREGISTERED),
    MAKE_SCODE_ENTRY(MK_S_REDUCED_TO_SELF),
    MAKE_SCODE_ENTRY(MK_S_US),
    MAKE_SCODE_ENTRY(OLE_S_MAC_CLIPFORMAT),
    MAKE_SCODE_ENTRY(OLE_S_STATIC),
    MAKE_SCODE_ENTRY(OLE_S_USEREG),
    MAKE_SCODE_ENTRY(OLEOBJ_S_CANNOT_DOVERB_NOW),
    MAKE_SCODE_ENTRY(OLEOBJ_S_INVALIDHWND),
    MAKE_SCODE_ENTRY(OLEOBJ_S_INVALIDVERB),
    MAKE_SCODE_ENTRY(OLEOBJ_S_LAST),
    MAKE_SCODE_ENTRY(STG_S_CONVERTED),
    MAKE_SCODE_ENTRY(VIEW_S_ALREADY_FROZEN),

    MAKE_SCODE_ENTRY(E_UNEXPECTED),
    MAKE_SCODE_ENTRY(E_NOTIMPL),
    MAKE_SCODE_ENTRY(E_OUTOFMEMORY),
    MAKE_SCODE_ENTRY(E_INVALIDARG),
    MAKE_SCODE_ENTRY(E_NOINTERFACE),
    MAKE_SCODE_ENTRY(E_POINTER),
    MAKE_SCODE_ENTRY(E_HANDLE),
    MAKE_SCODE_ENTRY(E_ABORT),
    MAKE_SCODE_ENTRY(E_FAIL),
    MAKE_SCODE_ENTRY(E_ACCESSDENIED),

    MAKE_SCODE_ENTRY(CACHE_E_NOCACHE_UPDATED),
    MAKE_SCODE_ENTRY(CLASS_E_CLASSNOTAVAILABLE),
    MAKE_SCODE_ENTRY(CLASS_E_NOAGGREGATION),
    MAKE_SCODE_ENTRY(CLIPBRD_E_BAD_DATA),
    MAKE_SCODE_ENTRY(CLIPBRD_E_CANT_CLOSE),
    MAKE_SCODE_ENTRY(CLIPBRD_E_CANT_EMPTY),
    MAKE_SCODE_ENTRY(CLIPBRD_E_CANT_OPEN),
    MAKE_SCODE_ENTRY(CLIPBRD_E_CANT_SET),
    MAKE_SCODE_ENTRY(CO_E_ALREADYINITIALIZED),
    MAKE_SCODE_ENTRY(CO_E_APPDIDNTREG),
    MAKE_SCODE_ENTRY(CO_E_APPNOTFOUND),
    MAKE_SCODE_ENTRY(CO_E_APPSINGLEUSE),
    MAKE_SCODE_ENTRY(CO_E_BAD_PATH),
    MAKE_SCODE_ENTRY(CO_E_CANTDETERMINECLASS),
    MAKE_SCODE_ENTRY(CO_E_CLASS_CREATE_FAILED),
    MAKE_SCODE_ENTRY(CO_E_CLASSSTRING),
    MAKE_SCODE_ENTRY(CO_E_DLLNOTFOUND),
    MAKE_SCODE_ENTRY(CO_E_ERRORINAPP),
    MAKE_SCODE_ENTRY(CO_E_ERRORINDLL),
    MAKE_SCODE_ENTRY(CO_E_IIDSTRING),
    MAKE_SCODE_ENTRY(CO_E_NOTINITIALIZED),
    MAKE_SCODE_ENTRY(CO_E_OBJISREG),
    MAKE_SCODE_ENTRY(CO_E_OBJNOTCONNECTED),
    MAKE_SCODE_ENTRY(CO_E_OBJNOTREG),
    MAKE_SCODE_ENTRY(CO_E_OBJSRV_RPC_FAILURE),
    MAKE_SCODE_ENTRY(CO_E_SCM_ERROR),
    MAKE_SCODE_ENTRY(CO_E_SCM_RPC_FAILURE),
    MAKE_SCODE_ENTRY(CO_E_SERVER_EXEC_FAILURE),
    MAKE_SCODE_ENTRY(CO_E_SERVER_STOPPING),
    MAKE_SCODE_ENTRY(CO_E_WRONGOSFORAPP),
    MAKE_SCODE_ENTRY(CONVERT10_E_OLESTREAM_BITMAP_TO_DIB),
    MAKE_SCODE_ENTRY(CONVERT10_E_OLESTREAM_FMT),
    MAKE_SCODE_ENTRY(CONVERT10_E_OLESTREAM_GET),
    MAKE_SCODE_ENTRY(CONVERT10_E_OLESTREAM_PUT),
    MAKE_SCODE_ENTRY(CONVERT10_E_STG_DIB_TO_BITMAP),
    MAKE_SCODE_ENTRY(CONVERT10_E_STG_FMT),
    MAKE_SCODE_ENTRY(CONVERT10_E_STG_NO_STD_STREAM),
    MAKE_SCODE_ENTRY(DISP_E_ARRAYISLOCKED),
    MAKE_SCODE_ENTRY(DISP_E_BADCALLEE),
    MAKE_SCODE_ENTRY(DISP_E_BADINDEX),
    MAKE_SCODE_ENTRY(DISP_E_BADPARAMCOUNT),
    MAKE_SCODE_ENTRY(DISP_E_BADVARTYPE),
    MAKE_SCODE_ENTRY(DISP_E_EXCEPTION),
    MAKE_SCODE_ENTRY(DISP_E_MEMBERNOTFOUND),
    MAKE_SCODE_ENTRY(DISP_E_NONAMEDARGS),
    MAKE_SCODE_ENTRY(DISP_E_NOTACOLLECTION),
    MAKE_SCODE_ENTRY(DISP_E_OVERFLOW),
    MAKE_SCODE_ENTRY(DISP_E_PARAMNOTFOUND),
    MAKE_SCODE_ENTRY(DISP_E_PARAMNOTOPTIONAL),
    MAKE_SCODE_ENTRY(DISP_E_TYPEMISMATCH),
    MAKE_SCODE_ENTRY(DISP_E_UNKNOWNINTERFACE),
    MAKE_SCODE_ENTRY(DISP_E_UNKNOWNLCID),
    MAKE_SCODE_ENTRY(DISP_E_UNKNOWNNAME),
    MAKE_SCODE_ENTRY(DRAGDROP_E_ALREADYREGISTERED),
    MAKE_SCODE_ENTRY(DRAGDROP_E_INVALIDHWND),
    MAKE_SCODE_ENTRY(DRAGDROP_E_NOTREGISTERED),
    MAKE_SCODE_ENTRY(DV_E_CLIPFORMAT),
    MAKE_SCODE_ENTRY(DV_E_DVASPECT),
    MAKE_SCODE_ENTRY(DV_E_DVTARGETDEVICE),
    MAKE_SCODE_ENTRY(DV_E_DVTARGETDEVICE_SIZE),
    MAKE_SCODE_ENTRY(DV_E_FORMATETC),
    MAKE_SCODE_ENTRY(DV_E_LINDEX),
    MAKE_SCODE_ENTRY(DV_E_NOIVIEWOBJECT),
    MAKE_SCODE_ENTRY(DV_E_STATDATA),
    MAKE_SCODE_ENTRY(DV_E_STGMEDIUM),
    MAKE_SCODE_ENTRY(DV_E_TYMED),
    MAKE_SCODE_ENTRY(INPLACE_E_NOTOOLSPACE),
    MAKE_SCODE_ENTRY(INPLACE_E_NOTUNDOABLE),
    MAKE_SCODE_ENTRY(MEM_E_INVALID_LINK),
    MAKE_SCODE_ENTRY(MEM_E_INVALID_ROOT),
    MAKE_SCODE_ENTRY(MEM_E_INVALID_SIZE),
    MAKE_SCODE_ENTRY(MK_E_CANTOPENFILE),
    MAKE_SCODE_ENTRY(MK_E_CONNECTMANUALLY),
    MAKE_SCODE_ENTRY(MK_E_ENUMERATION_FAILED),
    MAKE_SCODE_ENTRY(MK_E_EXCEEDEDDEADLINE),
    MAKE_SCODE_ENTRY(MK_E_INTERMEDIATEINTERFACENOTSUPPORTED),
    MAKE_SCODE_ENTRY(MK_E_INVALIDEXTENSION),
    MAKE_SCODE_ENTRY(MK_E_MUSTBOTHERUSER),
    MAKE_SCODE_ENTRY(MK_E_NEEDGENERIC),
    MAKE_SCODE_ENTRY(MK_E_NO_NORMALIZED),
    MAKE_SCODE_ENTRY(MK_E_NOINVERSE),
    MAKE_SCODE_ENTRY(MK_E_NOOBJECT),
    MAKE_SCODE_ENTRY(MK_E_NOPREFIX),
    MAKE_SCODE_ENTRY(MK_E_NOSTORAGE),
    MAKE_SCODE_ENTRY(MK_E_NOTBINDABLE),
    MAKE_SCODE_ENTRY(MK_E_NOTBOUND),
    MAKE_SCODE_ENTRY(MK_E_SYNTAX),
    MAKE_SCODE_ENTRY(MK_E_UNAVAILABLE),
    MAKE_SCODE_ENTRY(OLE_E_ADVF),
    MAKE_SCODE_ENTRY(OLE_E_ADVISENOTSUPPORTED),
    MAKE_SCODE_ENTRY(OLE_E_BLANK),
    MAKE_SCODE_ENTRY(OLE_E_CANT_BINDTOSOURCE),
    MAKE_SCODE_ENTRY(OLE_E_CANT_GETMONIKER),
    MAKE_SCODE_ENTRY(OLE_E_CANTCONVERT),
    MAKE_SCODE_ENTRY(OLE_E_CLASSDIFF),
    MAKE_SCODE_ENTRY(OLE_E_ENUM_NOMORE),
    MAKE_SCODE_ENTRY(OLE_E_INVALIDHWND),
    MAKE_SCODE_ENTRY(OLE_E_INVALIDRECT),
    MAKE_SCODE_ENTRY(OLE_E_NOCACHE),
    MAKE_SCODE_ENTRY(OLE_E_NOCONNECTION),
    MAKE_SCODE_ENTRY(OLE_E_NOSTORAGE),
    MAKE_SCODE_ENTRY(OLE_E_NOT_INPLACEACTIVE),
    MAKE_SCODE_ENTRY(OLE_E_NOTRUNNING),
    MAKE_SCODE_ENTRY(OLE_E_OLEVERB),
    MAKE_SCODE_ENTRY(OLE_E_PROMPTSAVECANCELLED),
    MAKE_SCODE_ENTRY(OLE_E_STATIC),
    MAKE_SCODE_ENTRY(OLE_E_WRONGCOMPOBJ),
    MAKE_SCODE_ENTRY(OLEOBJ_E_INVALIDVERB),
    MAKE_SCODE_ENTRY(OLEOBJ_E_NOVERBS),
    MAKE_SCODE_ENTRY(REGDB_E_CLASSNOTREG),
    MAKE_SCODE_ENTRY(REGDB_E_IIDNOTREG),
    MAKE_SCODE_ENTRY(REGDB_E_INVALIDVALUE),
    MAKE_SCODE_ENTRY(REGDB_E_KEYMISSING),
    MAKE_SCODE_ENTRY(REGDB_E_READREGDB),
    MAKE_SCODE_ENTRY(REGDB_E_WRITEREGDB),
    MAKE_SCODE_ENTRY(RPC_E_ATTEMPTED_MULTITHREAD),
    MAKE_SCODE_ENTRY(RPC_E_CALL_CANCELED),
    MAKE_SCODE_ENTRY(RPC_E_CALL_REJECTED),
    MAKE_SCODE_ENTRY(RPC_E_CANTCALLOUT_AGAIN),
    MAKE_SCODE_ENTRY(RPC_E_CANTCALLOUT_INASYNCCALL),
    MAKE_SCODE_ENTRY(RPC_E_CANTCALLOUT_INEXTERNALCALL),
    MAKE_SCODE_ENTRY(RPC_E_CANTCALLOUT_ININPUTSYNCCALL),
    MAKE_SCODE_ENTRY(RPC_E_CANTPOST_INSENDCALL),
    MAKE_SCODE_ENTRY(RPC_E_CANTTRANSMIT_CALL),
    MAKE_SCODE_ENTRY(RPC_E_CHANGED_MODE),
    MAKE_SCODE_ENTRY(RPC_E_CLIENT_CANTMARSHAL_DATA),
    MAKE_SCODE_ENTRY(RPC_E_CLIENT_CANTUNMARSHAL_DATA),
    MAKE_SCODE_ENTRY(RPC_E_CLIENT_DIED),
    MAKE_SCODE_ENTRY(RPC_E_CONNECTION_TERMINATED),
    MAKE_SCODE_ENTRY(RPC_E_DISCONNECTED),
    MAKE_SCODE_ENTRY(RPC_E_FAULT),
    MAKE_SCODE_ENTRY(RPC_E_INVALID_CALLDATA),
    MAKE_SCODE_ENTRY(RPC_E_INVALID_DATA),
    MAKE_SCODE_ENTRY(RPC_E_INVALID_DATAPACKET),
    MAKE_SCODE_ENTRY(RPC_E_INVALID_PARAMETER),
    MAKE_SCODE_ENTRY(RPC_E_INVALIDMETHOD),
    MAKE_SCODE_ENTRY(RPC_E_NOT_REGISTERED),
    MAKE_SCODE_ENTRY(RPC_E_OUT_OF_RESOURCES),
    MAKE_SCODE_ENTRY(RPC_E_RETRY),
    MAKE_SCODE_ENTRY(RPC_E_SERVER_CANTMARSHAL_DATA),
    MAKE_SCODE_ENTRY(RPC_E_SERVER_CANTUNMARSHAL_DATA),
    MAKE_SCODE_ENTRY(RPC_E_SERVER_DIED),
    MAKE_SCODE_ENTRY(RPC_E_SERVER_DIED_DNE),
    MAKE_SCODE_ENTRY(RPC_E_SERVERCALL_REJECTED),
    MAKE_SCODE_ENTRY(RPC_E_SERVERCALL_RETRYLATER),
    MAKE_SCODE_ENTRY(RPC_E_SERVERFAULT),
    MAKE_SCODE_ENTRY(RPC_E_SYS_CALL_FAILED),
    MAKE_SCODE_ENTRY(RPC_E_THREAD_NOT_INIT),
    MAKE_SCODE_ENTRY(RPC_E_UNEXPECTED),
    MAKE_SCODE_ENTRY(RPC_E_WRONG_THREAD),
    MAKE_SCODE_ENTRY(STG_E_ABNORMALAPIEXIT),
    MAKE_SCODE_ENTRY(STG_E_ACCESSDENIED),
    MAKE_SCODE_ENTRY(STG_E_CANTSAVE),
    MAKE_SCODE_ENTRY(STG_E_DISKISWRITEPROTECTED),
    MAKE_SCODE_ENTRY(STG_E_EXTANTMARSHALLINGS),
    MAKE_SCODE_ENTRY(STG_E_FILEALREADYEXISTS),
    MAKE_SCODE_ENTRY(STG_E_FILENOTFOUND),
    MAKE_SCODE_ENTRY(STG_E_INSUFFICIENTMEMORY),
    MAKE_SCODE_ENTRY(STG_E_INUSE),
    MAKE_SCODE_ENTRY(STG_E_INVALIDFLAG),
    MAKE_SCODE_ENTRY(STG_E_INVALIDFUNCTION),
    MAKE_SCODE_ENTRY(STG_E_INVALIDHANDLE),
    MAKE_SCODE_ENTRY(STG_E_INVALIDHEADER),
    MAKE_SCODE_ENTRY(STG_E_INVALIDNAME),
    MAKE_SCODE_ENTRY(STG_E_INVALIDPARAMETER),
    MAKE_SCODE_ENTRY(STG_E_INVALIDPOINTER),
    MAKE_SCODE_ENTRY(STG_E_LOCKVIOLATION),
    MAKE_SCODE_ENTRY(STG_E_MEDIUMFULL),
    MAKE_SCODE_ENTRY(STG_E_NOMOREFILES),
    MAKE_SCODE_ENTRY(STG_E_NOTCURRENT),
    MAKE_SCODE_ENTRY(STG_E_NOTFILEBASEDSTORAGE),
    MAKE_SCODE_ENTRY(STG_E_OLDDLL),
    MAKE_SCODE_ENTRY(STG_E_OLDFORMAT),
    MAKE_SCODE_ENTRY(STG_E_PATHNOTFOUND),
    MAKE_SCODE_ENTRY(STG_E_READFAULT),
    MAKE_SCODE_ENTRY(STG_E_REVERTED),
    MAKE_SCODE_ENTRY(STG_E_SEEKERROR),
    MAKE_SCODE_ENTRY(STG_E_SHAREREQUIRED),
    MAKE_SCODE_ENTRY(STG_E_SHAREVIOLATION),
    MAKE_SCODE_ENTRY(STG_E_TOOMANYOPENFILES),
    MAKE_SCODE_ENTRY(STG_E_UNIMPLEMENTEDFUNCTION),
    MAKE_SCODE_ENTRY(STG_E_UNKNOWN),
    MAKE_SCODE_ENTRY(STG_E_WRITEFAULT),
    MAKE_SCODE_ENTRY(TYPE_E_AMBIGUOUSNAME),
    MAKE_SCODE_ENTRY(TYPE_E_BADMODULEKIND),
    MAKE_SCODE_ENTRY(TYPE_E_BUFFERTOOSMALL),
    MAKE_SCODE_ENTRY(TYPE_E_CANTCREATETMPFILE),
    MAKE_SCODE_ENTRY(TYPE_E_CANTLOADLIBRARY),
    MAKE_SCODE_ENTRY(TYPE_E_CIRCULARTYPE),
    MAKE_SCODE_ENTRY(TYPE_E_DLLFUNCTIONNOTFOUND),
    MAKE_SCODE_ENTRY(TYPE_E_DUPLICATEID),
    MAKE_SCODE_ENTRY(TYPE_E_ELEMENTNOTFOUND),
    MAKE_SCODE_ENTRY(TYPE_E_INCONSISTENTPROPFUNCS),
    MAKE_SCODE_ENTRY(TYPE_E_INVALIDSTATE),
    MAKE_SCODE_ENTRY(TYPE_E_INVDATAREAD),
    MAKE_SCODE_ENTRY(TYPE_E_IOERROR),
    MAKE_SCODE_ENTRY(TYPE_E_LIBNOTREGISTERED),
    MAKE_SCODE_ENTRY(TYPE_E_NAMECONFLICT),
    MAKE_SCODE_ENTRY(TYPE_E_OUTOFBOUNDS),
    MAKE_SCODE_ENTRY(TYPE_E_QUALIFIEDNAMEDISALLOWED),
    MAKE_SCODE_ENTRY(TYPE_E_REGISTRYACCESS),
    MAKE_SCODE_ENTRY(TYPE_E_SIZETOOBIG),
    MAKE_SCODE_ENTRY(TYPE_E_TYPEMISMATCH),
    MAKE_SCODE_ENTRY(TYPE_E_UNDEFINEDTYPE),
    MAKE_SCODE_ENTRY(TYPE_E_UNKNOWNLCID),
    MAKE_SCODE_ENTRY(TYPE_E_UNSUPFORMAT),
    MAKE_SCODE_ENTRY(TYPE_E_WRONGTYPEKIND),
    MAKE_SCODE_ENTRY(VIEW_E_DRAW),
    { 0, NULL }
  };
  #undef MAKE_SCODE_ENTRY
  int i;

  // look for it in the table
  for (i=0; scodeTable[i].szNameW!=NULL; i++)
  {
    if (sc == scodeTable[i].sc)
      return scodeTable[i].szNameW;
  }
  _snwprintf_s(szTempBufW, 32, _TRUNCATE, L"%#08.8x", sc);
  return szTempBufW;
}
